package 富婆梦.第一阶段.第六周.作业.俄罗斯转盘;

import java.util.LinkedList;
import java.util.Scanner;

/**
 * @ProjectName: java-learning
 * @Package: 富婆梦.第一阶段.第五周.作业.俄罗斯转盘
 * @ClassName: TestGame
 * @Author: hemengjin
 * @Description: 游戏开始类
 * 1、游戏开始后，每次有人死了，都可以进行存档操作
 * 2、存档内容为：所有玩家list，活着的玩家排序过后list
 * 3、如果继续游戏要读档
 * @Date:2021/8/22 18:23
 * @Version: 1.0
 */
public class TestGame {
    int count = 0;//开枪次数统计
    //四个玩家
    static LinkedList<Player> players = new LinkedList<>();

    public static void main(String[] args) {
        TestGame test = new TestGame();
        test.players.add(new Player("小A"));
        test.players.add(new Player("小B"));
        test.players.add(new Player("小C"));
        test.players.add(new Player("小D"));

        //枪里面随机放3颗子弹
        Gun.test1();
        //转动转轮
        Gun.turn();
        System.out.println("游戏开始前：");
        for (Player p1 : test.players) {
            System.out.println(p1.getName() + "是否活着" + p1.isLife());
        }

        System.out.println("--------游戏正式开始--------");

        while (true) {
            test.takeTurn(test.players);
            isPreserve(players);
            if (Gun.getBullet() == 0) {
                System.out.println("-------------------游戏结束--------------");
                for (Player p1 : test.players) {
                    System.out.println(p1.getName() + "是否活着" + p1.isLife());
                }
                break;
            }
        }
    }

    //玩家列表中的玩家轮流对着自己开枪，一旦有人死，更新玩家列表，询问是否要存档，暂停游戏
    public void takeTurn(LinkedList<Player> player) {
        LinkedList<Player> temp = new LinkedList<>();
        for (int i = 0; i < player.size(); i++) {
            //活着的玩家才可以开枪
            if (player.get(i).isLife()) {
                count = count + 1;
                if (player.get(i).fire() == true) {
                    //中枪了，生命状态为false
                    player.get(i).gotShoot();
                    System.out.println("这是第" + count + "枪，开枪者为" + player.get(i).getName() + "死掉了，" + "还剩下" + Gun.getBullet() + "发子弹");
                    temp.addAll(Loop.queue(player, i));
                    player.clear();
                    player.addAll(temp);
                    break;
                } else {
                    System.out.println("这是第" + count + "枪，开枪者为" + player.get(i).getName() + "活下来了，" + "还剩下" + Gun.getBullet() + "发子弹");
                }

            }

        }
    }

    //player:还活着按照游戏规则排序后的玩家list
    //players:所有玩家list集合
    public static void isPreserve(LinkedList<Player> players) {
        System.out.println("是否要存档，暂停游戏？请输入：是/否");
        Scanner scanner = new Scanner(System.in);
        if (scanner.next().equals("是")) {
            Preserve.write(players);
            players.clear();
            isRead();
        }
    }

    public static void isRead() {
        System.out.println("是否要读档，继续游戏？请输入：是/否");
        Scanner scanner = new Scanner(System.in);
        if (scanner.next().equals("是")) {
            players = Preserve.read();
        } else {
            System.out.println("-----------游戏暂停中--------");
            isRead();
        }
    }
}
